package org.lagou.Dao.impl;

import java.util.List;

import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.lagou.Dao.ResumeDao;
import org.lagou.bean.ResumeBean;
import org.lagou.resultsethandler.ResumeBeanResultsethandler;

public class ResumeDaoImpl extends BaseDao implements ResumeDao {

	@Override
	public List<ResumeBean> getResumeAll() {
		try {
			return query.query("select * from resume", new BeanListHandler<ResumeBean>(ResumeBean.class));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<ResumeBean> getResumePagin(int pageIndex, int pageSize) {
		try {
			return query.query("select * from resume limit ?,?", new BeanListHandler<ResumeBean>(ResumeBean.class),
					(pageIndex - 1) * pageSize, pageSize);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public long getResumeCount() {
		try {
			return query.query("select count(1) from resume", new ScalarHandler<Long>());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<ResumeBean> getResumeById(int id) {
		try {
			return query.query("select * from resume where id=?", new BeanListHandler<ResumeBean>(ResumeBean.class),
					id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	

	@Override
	public List<ResumeBean> getResumeByUserid(int userid) {
		try {
			return query.query("select * from resume where userid=?", new BeanListHandler<ResumeBean>(ResumeBean.class),
					userid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public int addResume(ResumeBean info) {
		try {
			return query.update(
					"insert into resume(resumename,userid,name,sex,education,date) values(?,?,?,?,?,?)",
					info.getResumename(), info.getUserid(), info.getName(),info.getSex(),info.getEducation(),info.getDate());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateResume(ResumeBean info) {
		try {
			return query.update(
					"update resume set resumename=?,userid=?,name=?,sex=?,education=?,edexperience=?,tel=?,email=?,state=?,img=?,city=?,nature=?,rposition=?,salary=?,oldcompany=?,oldposition=?,cbeginyear=?,cbeginmonth=?,cendyear=?,cendmouth=?,project=?,role=?,pbeginyear=?,pbeginmonth=?,pendyear=?,pendmouth=?,rdescribe=?,school=?,major=?,beginyear=?,endyear=?,introduce=?,url=?,rexplain=?,date=? where id=?",
					info.getResumename(), info.getUserid(), info.getName(), info.getSex(), info.getEducation(),
					info.getEdexperience(), info.getTel(), info.getEmail(), info.getState(), info.getImg(),
					info.getCity(), info.getNature(), info.getRposition(), info.getSalary(), info.getOldcompany(),
					info.getOldposition(), info.getCbeginyear(), info.getCbeginmonth(), info.getCendyear(),
					info.getCendmonth(), info.getProject(), info.getRole(), info.getPbeginyear(), info.getPbeginmonth(),
					info.getPendyear(), info.getPendmonth(), info.getRdescribe(), info.getSchool(), info.getMajor(),
					info.getBeginyear(), info.getEndyear(), info.getIntroduce(), info.getUrl(), info.getRexplain(),
					info.getDate(), info.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int deleteResumeById(int id) {
		try {
			return query.update("delete from resume where id=?", id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateImg(ResumeBean info) {
		try {
			return query.update("update resume set img=?,date=? where id=?", info.getImg(), info.getDate(),
					info.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateResumeName(ResumeBean info) {
		try {
			return query.update("update resume set resumename=? where id=?", info.getResumename(),
					 info.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateUserInformation(ResumeBean info) {
		try {
			return query.update(
					"update resume set name=?,sex=?,education=?,edexperience=?,email=?,state=?,date=? where id=?",
					info.getName(), info.getSex(), info.getEducation(), info.getEdexperience(), info.getEmail(),
					info.getState(), info.getDate(), info.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateExpectWork(ResumeBean info) {
		try {
			return query.update("update resume set city=?,nature=?,rposition=?,salary=?,date=? where id=?",
					info.getCity(), info.getNature(), info.getRposition(), info.getSalary(), info.getDate(),
					info.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateWorkExperience(ResumeBean info) {
		try {
			return query.update(
					"update resume set oldcompany=?,oldposition=?,cbeginyear=?,cbeginmonth=?,cendyear=?,cendmonth=?,date=? where id=?",
					info.getOldcompany(), info.getOldposition(), info.getCbeginyear(), info.getCbeginmonth(),
					info.getCendyear(), info.getCendmonth(), info.getDate(), info.getId());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateProjectExperience(ResumeBean info,int id) {
		try {
			return query.update(
					"update resume set project=?,role=?,pbeginyear=?,pbeginmonth=?,pendyear=?,pendmonth=?,rdescribe=?,date=? where id=?",
					info.getProject(), info.getRole(), info.getPbeginyear(), info.getPbeginmonth(), info.getPendyear(),
					info.getPendmonth(), info.getRdescribe(), info.getDate(), id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateEducationBackground(ResumeBean info,int id) {
		try {
			return query.update(
					"update resume set school=?,education=?,major=?,beginyear=?,endyear=?,date=? where id=?",
					info.getSchool(), info.getEducation(), info.getMajor(), info.getBeginyear(), info.getEndyear(),
					info.getDate(), id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateSelfDescription(ResumeBean info,int id) {
		try {
			return query.update("update resume set introduce=?,date=? where id=?", info.getIntroduce(), info.getDate(),
					id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public int updateWorksShow(ResumeBean info,int id) {
		try {
			return query.update("update resume set url=?,rexplain=?,date=? where id=?", info.getUrl(),
					info.getRexplain(), info.getDate(),id);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return -1;
	}

	@Override
	public List<ResumeBean> getResumeByResumeid(int resumeid) {
		try {
			return query.query("select g.*,r.date,r.salary,r.city,r.userid from getresume g,resume r where r.userid=g.userid and r.id=?", new ResumeBeanResultsethandler(),
					resumeid);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public List<ResumeBean> getResumeByState(int userid, int state) {
		try {
			return query.query("select g.*,r.date,r.salary,r.city,r.userid from getresume g,resume r where r.userid=g.userid and r.userid=? and g.state=?", new ResumeBeanResultsethandler(),
					userid,state);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

}
